package xyz.hubery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MimeTypeUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.EntrustOrder;
import xyz.hubery.domain.Market;
import xyz.hubery.model.R;
import xyz.hubery.service.EntrustOrderService;
import xyz.hubery.service.MarketService;
import xyz.hubery.stream.Source;
import xyz.hubery.vo.TradeEntrustOrderVo;

@RestController
@RequestMapping("/entrustOrders")
@Api(tags = "委托订单")
public class EntrustOrderController {

    @Autowired
    private EntrustOrderService entrustOrderService;


    @GetMapping
    @ApiOperation("获取用户的委托订单")
    public R<Page<EntrustOrder>> findByPage(Page<EntrustOrder> page, String symbol, Integer type) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Page<EntrustOrder> pageData = entrustOrderService.findByPage(page, userId, symbol, type);
        return R.ok(pageData);
    }

    @GetMapping("/history/{symbol}")
    @ApiOperation("查询用户的委托单记录")
    public R<Page<TradeEntrustOrderVo>> findHistoryTradeEntrustOrder(Page<EntrustOrder> page, @PathVariable String symbol) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Page<TradeEntrustOrderVo> voPage = entrustOrderService.findHistoryTradeEntrustOrder(page, userId, symbol);
        return R.ok(voPage);
    }


    @GetMapping("/{symbol}")
    @ApiOperation("查询用户未完成的委托单记录")
    public R<Page<TradeEntrustOrderVo>> findTradeEntrustOrder(Page<EntrustOrder> page, @PathVariable String symbol) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Page<TradeEntrustOrderVo> voPage = entrustOrderService.findTradeEntrustOrder(page, userId, symbol);
        return R.ok(voPage);
    }


    @PostMapping
    @ApiOperation("发起一个委托单")
    public R addEntrustOrder(@RequestBody @Validated EntrustOrder entrustOrder) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        entrustOrder.setUserId(userId);
        // 创建一个委托单
        boolean save = entrustOrderService.createEntrustOrder(entrustOrder);
        return save ? R.ok() : R.fail("买入失败");
    }


    @DeleteMapping("/{id}")
    @ApiOperation("撤销一个委托")
    public R delEntrustOrder(@PathVariable Long id) {
        entrustOrderService.sendCancelOrderMessage(id);
        return  R.ok("订单删除中");
    }



}
